# -*- coding: utf-8 -*-
#   Copyright 2009, 2010 Thomas Olsen <tanghus@gmail.com>
#
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU Library General Public License as
#   published by the Free Software Foundation; either version 2 or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#
#   GNU General Public License for more details
#
#
#   You should have received a copy of the GNU Library General Public
#   License along with this program; if not, write to the
#   Free Software Foundation, Inc.,
#
#   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
#
#   Inspiration from http://ubuntuforums.org/showthread.php?t=1087457

from PyQt4 import QtCore
from PyQt4.QtCore import  QVariant, QSize, SIGNAL
from PyKDE4.kdecore import i18n
from PyKDE4.kdeui import KIcon
from PyKDE4.plasma import Plasma
import os
from GoogleTranslator import GoogleTranslator
from NetworkMonitor import NetworkMonitor
from Notifier import Notifier
from base import BasePlasmoid
from mylogging import debug

class GoogleTranslatorApplet(BasePlasmoid):

    def __init__(self,parent,args=None):
        self.parent = parent
        BasePlasmoid.__init__(self, parent)

    def init(self):
        debug("GoogleTranslatorApplet: init().")
        BasePlasmoid.init(self)

        self._widget = None
        self.setHasConfigurationInterface(False)
        self.setAspectRatioMode(Plasma.IgnoreAspectRatio)

        self.notifier = Notifier(self)
        self.nwmon = NetworkMonitor()
        if self.nwmon.connected():
          self.setBusy(False)
        else:
            self.notifier.notify("waiting-for-network", i18n("Waiting for network connection."))

        self.nwmon.status_changed.connect(self.netstate_changed)

        # Main widget
        debug("GoogleTranslatorApplet: Creating main widget.")
        self._widget = GoogleTranslator(self)
        self._widget.init()
        self.setGraphicsWidget(self._widget)
        self.setPopupIcon(self.metadata.pluginName())

        self.setGraphicsWidget(self._widget)

        # Tool tip for panel
        self.metadata = self.package().metadata()
        self.tooltipdata = Plasma.ToolTipContent()
        self.tooltipdata.setMainText(self.metadata.name())
        self.tooltipdata.setSubText(self.metadata.description())
        self.tooltipdata.setImage(KIcon(self.metadata.pluginName()))
        Plasma.ToolTipManager.self().setContent(self.applet, self.tooltipdata)

        # Only register the tooltip in panels
        #if (self.formFactor() != Plasma.Planar):
        if ((self.formFactor() == Plasma.Horizontal) or (self.formFactor() == Plasma.Vertical)):
            print("GoogleTranslatorApplet: In Panel")
            Plasma.ToolTipManager.self().registerWidget(self.applet)
        else:
            Plasma.ToolTipManager.self().unregisterWidget(self.applet)
            print("GoogleTranslatorApplet: Not in Panel")

        self.configChanged()

    def configChanged(self):
        debug("GoogleTranslator: configChanged")
        # TODO:
        #	Clear ComboBoxes
        #	Add languages
        #	Select languages
        #	Translate
        defaultSize =  QVariant(QSize (0,0))
        size = self.cfg.readEntry("size", defaultSize ).toSize()
        if (size != defaultSize) :
            self._widget.setPreferredSize(size.width(), size.height())
        else:
            self._widget.setPreferredSize(400 ,200)

        self.connect(self._widget, SIGNAL("resized()"), self.dialog_resized)

    @QtCore.pyqtSlot(name="dialog_resized")
    def dialog_resized(self):
        if self.isPopupShowing():
            self.config().writeEntry("size", QVariant(self.widget.size()))

    @QtCore.pyqtSlot(bool, name="netstate_changed")
    def netstate_changed(self, connected):
        print "GoogleTranslatorApplet: Network status changed! %i" % connected
        if connected:
            debug("GoogleTranslatorApplet: Connected!")
            self._widget.setEnabled(True)
            self.setBusy(False)
        else:
            self.notifier.notify("waiting-for-network", i18n("Waiting for network connection."))
            self._widget.setEnabled(False)
            debug("GoogleTranslatorApplet: Not connected!")
            self.setBusy(True)

def CreateApplet(parent):
  return GoogleTranslatorApplet(parent)